<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-3
 * Time: 下午2:16
 */

namespace App\Repositories\Portal;


use App\Models;

class TagRepository extends PortalRepository
{

    public function __construct()
    {
        parent::__construct();
        $this->model = new Models\Portal\Tag();
    }

    private static $instance;

    /**
     * 实现单例
     */
    public static function getInstance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * 声明实例模型
     * @param array $filter
     * @return mixed
     */
    public function getSelectModel(array $filter = []) : \Illuminate\Database\Eloquent\Builder
    {
        $model = $this->model;

        $model = $this->getSiteIdModel($model, $filter);

        //  标签类型
        if (isset($filter['type'])) {
            $model = $model->where('type', '=', (int) $filter['type']);
        }

        //  标签名称
        if (isset($filter['name'])) {
            $model = $model->where('name', 'like', '%'.$filter['name'].'%');
        }

        $model = $model->where('deleted_at', null);

        return $model;
    }

    /**
     * 获取后台分页
     * @param null $siteId
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getAdminPaginateData($siteId = null) : \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return $this->getSelectModel()->orderBy('id', 'desc')->paginate($this->pageSize);
    }

    /**
     * 获取指定条件的标签列表
     * @param int $type
     * @param null $siteId
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getTagListByType(int $type = 0, $siteId = null)
    {
        return $this->getSelectModel([
            'type' => $type,
            'site_id' => $siteId
        ])->orderBy('id', 'desc')->get();
    }
}